;layui.define(function(e) {
    layui.use(['table','layer','form','laydate'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;
        //展示已知数据
        table.render({
            elem: '#idTest'
            ,cols: [[ //标题栏
                {field: 'brand', title: '品牌'}
                ,{field: 'carSeries', title: '车系'}
                ,{field: 'carType', title: '车型', minWidth: 120}
                ,{field: 'customername', title: '客户姓名'}
                ,{field: 'sex', title: '性别'}
                ,{field: 'phonenumber', title: '手机号'}
                ,{field: 'customerlevel', title: '客户级别'}
                ,{field: 'orderdate', title: '订单日期',minWidth: 160}
                ,{field: 'deliverydate', title: '交车日期',minWidth: 160}
                ,{field: 'orderstatus', title: '订单状态'}
                ,{field: 'salessdvisor', title: '销售顾问'}
                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:188, align:'center', toolbar: '#barDemo'}
            ]]
            ,height: 'full-400'
            ,cellMinWidth: 120
            ,data: [{
                "brand": "大众"
                ,"carSeries": "郎逸"
                ,"carType": "2018款自动精英型"
                ,"customername": "Finn"
                ,"sex": "女"
                ,"phonenumber": "13897913622"
                ,"customerlevel": "01"
                ,"orderdate": "2018-10-23 13:23:32"
                ,"deliverydate":"2018-10-25 13:23:32"
                ,"orderstatus": "已交车"
                ,"salessdvisor":"龚天否"
                ,"tool": "2016-10-14"
            }]
            ,page: true //是否显示分页
        });

        //展示已知数据(车辆信息)
        table.render({
            elem: '#idTest1'
            ,cols: [[ //标题栏
                 {field: 'brand', title: '品牌'}
                ,{field: 'carSeries', title: '车系'}
                ,{field: 'carType', title: '车型', minWidth: 160}
                ,{field: 'customername', title: '指导价'}
                ,{field: 'sex', title: '采购价'}
                ,{field: 'phonenumber', title: '车类'}
                ,{field: 'customerlevel', title: '外观颜色'}
                ,{field: 'orderdate', title: '内饰颜色',}
                ,{field: 'deliverydate', title: '车架号',}
            ]]
            ,height: '100'
            ,cellMinWidth: 120
            ,data: [{
                "brand": "大众"
                ,"carSeries": "郎逸"
                ,"carType": "2018款自动精英型"
                ,"customername": "1249000"
                ,"sex": "107800"
                ,"phonenumber": "新能源"
                ,"customerlevel": "黑色"
                ,"orderdate": "棕色"
                ,"deliverydate":"LS5ADE0AB046793"
            }]
            ,page: false //是否显示分页
        });

        //展示已知数据(车辆信息)
        table.render({
            elem: '#idTest2'
            ,cols: [[ //标题栏
                {field: 'brand', title: '入库时间'}
                ,{field: 'carSeries', title: '库龄'}
                ,{field: 'carType', title: '库存状态', minWidth: 160}
                ,{field: 'customername', title: '预警状态'}
            ]]
            ,height: '100'
            ,cellMinWidth: 120
            ,data: [{
                "brand": "2018-10-23 12:32:34"
                ,"carSeries": "5"
                ,"carType": "在库"
                ,"customername": "1249000"
            }]
            ,page: false //是否显示分页
        });

        //展示已知数据(关联客户)
        table.render({
            elem: '#idTest3'
            ,cols: [[ //标题栏
                ,{type:'radio', fixed: 'left'}
                ,{field: 'customerName', title: '客户姓名'}
                ,{field: 'sex', title: '性别'}
                ,{field: 'phoneNumber', title: '手机号',}
                ,{field: 'customerType', title: '客户类型'}
                ,{field: 'informationSources', title: '信息来源'}
                ,{field: 'customerLevel', title: '客户等级'}
            ]]
            ,height: 'full-400'
            ,cellMinWidth: 120
            ,data: [{
                "customerName": "Finn"
                ,"sex": "女"
                ,"phoneNumber": "13897913622"
                ,"customerType": "个人客户"
                ,"informationSources": "线上App"
                ,"customerLevel": "01"
            },
            {
                "customerName": "李宇"
                ,"sex": "男"
                ,"phoneNumber": "13897913622"
                ,"customerType": "个人客户"
                ,"informationSources": "线上App"
                ,"customerLevel": "02"
            }]
            ,page: true //是否显示分页
        });

        //展示已知数据(关联车辆)
        table.render({
            elem: '#idTest4'
            ,cols: [[ //标题栏
                ,{type:'radio', fixed: 'left'}
                ,{field: 'brand', title: '品牌'}
                ,{field: 'carSystem', title: '车系'}
                ,{field: 'carModel', title: '车型',}
                ,{field: 'colour', title: '外观/内饰颜色'}
                ,{field: 'frameNumber', title: '车架号'}
                ,{field: 'storageTime', title: '入库时间'}
                ,{field: 'aging', title: '库龄'}
                ,{field: 'stateLibrary', title: '库存状态'}
                ,{field: 'warningState', title: '预警状态'}
            ]]
            ,height: 'full-400'
            ,cellMinWidth: 120
            ,data: [{
                 "brand": "通用别克1"
                ,"carSystem": "君越"
                ,"carModel": "2018款 2.0T舒适版"
                ,"colour": "黑色/棕色"
                ,"frameNumber": "LS5ADE0AB046793"
                ,"storageTime": "2018-10-23  13:24:32"
                ,"aging": "70"
                ,"stateLibrary": "在库"
                ,"warningState": "1"
            },
            {
                "brand": "通用别克2"
               ,"carSystem": "君越"
               ,"carModel": "2017款 1.0T舒适版"
               ,"colour": "黑色/棕色"
               ,"frameNumber": "LS5ADE0AB046793"
               ,"storageTime": "2018-10-23  13:24:32"
               ,"aging": "70"
               ,"stateLibrary": "在库"
               ,"warningState": "2"
           }]
            ,page: true //是否显示分页
        });

        //展示已知数据(上报审批通过)
        table.render({
            elem: '#idTest5'
            ,cols: [[ //标题栏
                ,{field: 'brand', title: '品牌',minWidth: 120}
                ,{field: 'carSystem', title: '车系',minWidth: 160}
                ,{field: 'carModel', title: '车型',minWidth: 160}
                ,{field: 'customerName', title: '客户姓名'}
                ,{field: 'sex', title: '性别'}
                ,{field: 'phoneNumber', title: '手机号',}
                ,{field: 'customerLevel', title: '客户级别'}
                ,{field: 'orderdate', title: '订单日期',minWidth: 160}
                ,{field: 'deliverydate', title: '交车日期',minWidth: 160}
                ,{field: 'orderstatus', title: '订单状态',minWidth: 120}
                ,{field: 'salessdvisor', title: '销售顾问'}
                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:180, align:'center', toolbar: '#barDemo'}
            ]]
            ,height: 'full-400'
            ,cellMinWidth: 120
            ,data: [{
                 "brand": "大众"
                ,"carSystem": "郎逸"
                ,"carModel": "2018款自动精英型"
                ,"customerName": "Finn"
                ,"sex": "女"
                ,"phoneNumber": "13897913622"
                ,"customerLevel": "01"
                ,"orderdate": "2018-10-23 13:23:32"
                ,"deliverydate":"2018-10-25 13:23:32"
                ,"orderstatus": "已交车"
                ,"salessdvisor":"龚天否"
                ,"tool": "2016-10-14"
            }]
            ,page: true //是否显示分页
        });

        //监听表格复选框选择
        table.on('checkbox(listTable)', function(obj){
            console.log(obj)
        });
        
        //监听工具条
        table.on('tool(listTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'details'){
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '详情'
                    ,content: 'details.html'
                    ,area: ['500px', '500px']
                    ,maxmin: true
                });
                layer.full(index);
            } else if(obj.event === 'rejectDetails'){
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '详情'
                    ,content: 'rejectDetails.html'
                    ,area: ['500px', '500px']
                    ,maxmin: true
                });
                layer.full(index);
            }
            else if(obj.event === 'soldOutShlef'){
                layer.confirm('确定销售上报?', function(index){
                   
                });
            } else if(obj.event === 'putOnShelf'){
                layui.alert("上架")
            }else if(obj.event === 'edit'){
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '编辑'
                    ,content: 'newedit _order.html'
                    ,area: ['500px', '500px']
                    ,maxmin: true
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //console.log(body.html()) //得到iframe页的body内容
                        var paramId=body.find("input[name='id']");
                        paramId.val(111);
                    }
                });
                layer.full(index);
            }
        });

        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.layadmin-takerates .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
         
        //执行一个laydate实例
        laydate.render({
            elem: '#date'
        });

        form.render();
        
        
         /**
          零售订单模块下的：新建编辑页（关联客户：页面逻辑处理）:开始
        **/

        //定义一个数组来接收表格中选中的数据
        var selectedCustomer = new Array();
        
        //关联车辆页面中:获取单选框的数据
        table.on('radio(glkhList)', function(obj){

            console.log(obj.checked); //当前是否选中状态

            //console.log(obj.data); //选中行的相关数据

            selectedCustomer = obj.data;

            console.log(selectedCustomer);
            
        });


        //关联车辆页面中：点击保存按钮
        $('#glkh-btnsave').click(function(){
            //调用本地保存关联车辆值的方法
            set1();  
            console.log('已点击保存');
            layer.close(layer.index);
        });
           
        function set1() {
                //由于是一个新的技术，你可以通过下面的代码检测你的浏览器是否支持
                if (window.localStorage) {
                    //存储变量的值
                    localStorage.setItem("selectedCustomer",JSON.stringify(selectedCustomer));
                    location.href = 'newedit _order.html';
                } else {
                   alert("NOT SUPPORT");
             }
        }

        /**
          零售订单模块下的：新建编辑页（关联客户：页面逻辑处理）:结束
        **/



       
        /**
          零售订单模块下的：新建编辑页（关联车辆：页面逻辑处理）:开始
        **/

        //定义一个数组来接收表格中选中的数据
        var selectedCars = new Array();
        
        //关联车辆页面中:获取单选框的数据
        table.on('radio(glclList)', function(obj){

            console.log(obj.checked); //当前是否选中状态

            //console.log(obj.data); //选中行的相关数据

            selectedCars = obj.data;

            console.log(selectedCars);

            layer.close(layer.index);
            
        });


        //关联车辆页面中：点击保存按钮
        $('#glcl-btnsave').click(function(){
            //调用本地保存关联车辆值的方法
            set2();  
            console.log('已点击保存');
        });
           
        function set2() {
                //由于是一个新的技术，你可以通过下面的代码检测你的浏览器是否支持
                if (window.localStorage) {
                    //存储变量的值
                    localStorage.setItem("selectedCars",JSON.stringify(selectedCars));
                    location.href = 'newedit _order.html';
                } else {
                    alert("NOT SUPPORT");
                }

        }

        /**
          零售订单模块下的：新建编辑页（关联车辆：页面逻辑处理）:结束
        **/


        //订单池：按钮“新建订单”
        $("#neweditOrder").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '新建订单'
                ,content: 'newedit _order.html'
                ,area: ['500px', '500px']
                ,maxmin: true
            });
            layer.full(index);
        });

        //客户信息：点击按钮“去关联”
        $("#associatedCustomers").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '关联客户'
                ,content: 'associatedCustomers.html'
                ,area: ['500px', '500px']
                ,maxmin: true
            });
            layer.full(index);
        });

        //车辆信息：点击按钮“去关联”
        $("#associatedVehicle").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '关联车辆'
                ,content: 'associatedVehicle.html'
                ,area: ['500px', '500px']
                ,maxmin: true
            });
            layer.full(index);
        })

         //车辆信息：点击按钮“新建客户”
         $("#newCustomers").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '新建客户'
                ,content: 'newCustomers.html'
                ,area: ['500px', '500px']
                ,maxmin: true
            });
            layer.full(index);
        });

    });
    
    //输出模块
    e("list", {})
});